var app = getApp(), fetchApi = require("../../utils/httpPost.js"), i = require("../../utils/arrayUtil.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.getImgUrl(),
    inputVal: "",
    searchList: [],
    searchResult: !1,
    hasConcent: !0,
    hotWordList: [],
    historyArray: [],
    pageindex: 1,
    pagemax: 1,
    showLoading: !1,
    searchContent: "",
    keyWordList: [],
    isEnd: !1,
    splitstart: 0,
    lastpg: !1,
    lastList: [],
    defaultWord: "搜索您需要的商品",
    isLoading: false,
    isiOs: !0
  },
  goBackPreRoute: function () {
    wx.navigateBack({ delta: 1 });
  },
  pushToAd: function (a) {
    d.pushToAd(a, "search");
  },
  pushToDetail_2: function (t) {
    console.log("进入详情页", t);
    var d = t.detail.target.dataset.item.id, s = t.detail.formId;
    // e.advertisingClick(s, 1, d), a.shareInfo("search", "appSearch", this.data.inputVal, d);
    var i = "/pages/product/detail/index?id=" + d + "&noSku=noSku&_from=appSearch&param1=" + this.data.inputVal;
    wx.navigateTo({ url: i });
  },
  bindClearHistory: function () {
    var that = this;
    wx.removeStorage({
      key: "historySearch",
      success: function (t) {
        console.log("清空历史搜索：", t),
          that.setData({
            historyArray: []
          });
      }
    });
  },
  saveHistorySearch: function (a) {
    var that = this, e = this.data.historyArray;
    e.map(function (t, d) {
      t == a && e.splice(d, 1);
    }), e.push(a),
      wx.setStorage({
        key: "historySearch",
        data: e,
        success: function () {
          that.setData({
            historyArray: e
          });
        }
      });
  },
  searchHisItem: function (a) {
    console.log("点击hisitem:", a.currentTarget.dataset.hisitem), this.setData({
      inputVal: a.currentTarget.dataset.hisitem,
      pageindex: 1,
      searchList: [],
      splitstart: 0,
      lastList: [],
      isLoading: !0
    }), this.fetchSearchData(a.currentTarget.dataset.hisitem, 1);
  },
  bindSearch: function () {
    this.setData({
      pageindex: 1,
      searchList: [],
      splitstart: 0,
      lastList: []
    })
    this.data.inputVal.length > 0 || this.data.defaultWord
        ? (console.log("调用搜索"), this.setData({ isLoading: !0 }), this.fetchSearchData(this.data.inputVal || this.data.defaultWord, 1))
        : wx.showModal({
          title: "提示",
          content: "请输入搜索内容",
          showCancel: !1
        });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var d = this;
    // this.onHttpInit()
    // d.setData({ defaultWord: options.defaultWord})
    wx.getStorage({
      key: "historySearch",
      success: function (a) {
        console.log("historySearch:", a), 
        d.setData({ 
          historyArray: a.data  
        });
      }
    })
  },
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost("/index/hotkeyword", {}, function (a) {
      console.log("keyWord:", a)
      that.setData({
        hotWordList: a.data.data.words,
        adBanner: a.data.data.banner,
        hasBanner: a.data.data.hasBanner,
        isLoading: false
      });
    }, function (a) {
      console.log("keyWork err:", a);
    });
  },
  serchOtherWord: function (a) {
    var t = a.currentTarget.dataset.word;
    this.setData({
      inputVal: t,
      pageindex: 1,
      searchList: [],
      splitstart: 0,
      lastList: [],
      isLoading: !0
    })
    this.fetchSearchData(t, 1);
  },
  fetchSearchData: function (e, d) {
    console.log('fetchSearchData ,e', e)
    console.log('fetchSearchData ,d', d)
    var o = this;
    if (this.data.searchList && this.data.searchList.length < 10 && d > 1) 
    return this.setData({
      showLoading: !1,
      isEnd: !0
    });
    var r = {
      keyword: e,
      pageIndex: o.data.pageindex,
      pageSize:10
    }
    o.saveHistorySearch(e)
    console.log("请求参数：", r), //a.analytics.searchKeyword(e),
      fetchApi.httpPost("/product/list", r, function (a) {
        console.log("search data:", a)
        if (a.data.code == 0)
          if (a.data.data.length > 0 && !a.data.data.recommend) {
            var r = [], h = a.data.data.otherWord && a.data.data.otherWord.length > 0 ? a.data.data.otherWord : "";
            r = a.data.data.aditem && a.data.data.aditem.length > 0 ? a.data.data.aditem[0].item 
            ? a.data.data : a.data.data.aditem.concat(a.data.data) : a.data.data,
            console.log('r', r)
            console.log('d', d)
            o.setData({
              searchList: 1 == d ? r : o.data.searchList.concat(r),
              searchResult: true,
              hasConcent: true,
              isLoading:false,
              showLoading: !1,
              searchContent: e,
              recommendWord: a.data.data.recommendWord ? a.data.data.recommendWord : "",
              otherWord: h,
            })
          }else{
            console.log('没数据')
            o.setData({
              searchResult: true,
              hasConcent: false,
              isLoading: false,
            })
          }
      }, function (a) {
        o.setData({ isLoading: !1 }),
        console.log("search data err:", a);
      });
  },
  clearInput: function () {
    this.setData({ inputVal: "" });
  },
  inputTyping: function (a) {
    console.log("输入框：", a.detail.value), 
    this.setData({ inputVal: a.detail.value })
    this.fetchSearchKeyword(a.detail.value);
  },
  fetchSearchKeyword: function (t) {
    var e = this;
    console.log("keyword:", t),
      fetchApi.httpPost("/index/search", {
        key_word: t
      }, function (a) {
        console.log("keyWord:", a),
          e.setData({
            keyWordList: a.data.data
          });
      }, function (a) {
        console.log("keyWork err:", a);
      });
  },
  cancleInput: function () {
    this.data.searchList.length > 0 ? this.setData({
      searchResult: !0
    }) : wx.navigateBack({
      delta: 1
    });
  },
  touchSearch: function () {
    this.setData({ searchResult: !1 });
  },
  onReachBottom: function () {
    console.log("上啦加载"), this.data.lastpg ? this.setData({
      showLoading: !1,
      isEnd: !0
    }) : (this.setData({
      showLoading: !0,
      isEnd: !1
    }),
      !(!this.data.inputVal && !this.data.searchContent) && this.fetchSearchData(this.data.inputVal || this.data.searchContent, this.data.pageindex + 1));
  }
})